package com.example.happytime;


import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class JokeFragment extends BaseFragment {

    PullToRefreshListView jokeLv;
    List<String> data;
    ArrayAdapter<String> adapter;
    int page = 1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view   =  inflater.inflate(R.layout.fragment_joke, container, false);
        jokeLv = view.findViewById(R.id.joke_lv);
        //数据源
        data = new ArrayList<>();
        //适配器：当布局只有一个TexiView时可以使用这个适配器
         adapter=new ArrayAdapter<>(getContext(),R.layout.item_joke,R.id.item_joke_tv,data);
         jokeLv.setAdapter(adapter);
         String url = getJokeUrl(page);
         //联网
            connectNetwork(url);
            setRefresh();
            return view;
    }
    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what == 1){
                jokeLv.onRefreshComplete();
            }
        }
    };
    private void setRefresh() {
        jokeLv.setMode(PullToRefreshBase.Mode.BOTH);
        jokeLv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                handler.sendEmptyMessageDelayed(1,500);//刷新完进度条不显示
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                String jokeUrl = getJokeUrl(page);
                connectNetwork(jokeUrl);
            }
        });
    }

    //获取笑话页面网址的方法
    private String getJokeUrl(int page) {
        String key = "aa43f18b9e0b5b27d950d23224ae7072";//可以换成自己的key
        String url = "http://v.juhe.cn/joke/content/list.php?key="+key+"&page="+page+"&pagesize=10&sort=asc&time=1418745237";
        return url;
    }
    //联网成功之后会执行的代码
    @Override
    public void onSuccess(String result) {
        JokeBean jokeBean = new Gson().fromJson(result, JokeBean.class);
        List<JokeBean.ResultBean.DataBean> list = jokeBean.getResult().getData();
        for (int i = 0;i < list.size();i++){
            String s = list.get(i).getContent();
            data.add(s);
        }
        adapter.notifyDataSetChanged();
        jokeLv.onRefreshComplete();
    }
}
